﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;

namespace WebVideo.Areas.Admin.Models
{
    public class Image_VideoModel
    {
        public int Id { get; set; }
        public string Image { get; set; }
        public int IdVideo { get; set; }
        public int IdQuestion { get; set; }
        public IEnumerable<EntityQuestions_MultipleChoice> DSQuestion { get; set; }
        

        public static IEnumerable<EntityImage_Video > LayDuLieu(int IdEdit)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityImage_Videos 
                        where p.IdVideo == IdEdit
                        select p;

            return query.ToList();
        }
        public static string Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityImage_Video tnew = db.EntityImage_Videos.Where(p => p.Id == Id).Single();
            db.EntityImage_Videos.DeleteOnSubmit(tnew);
            db.SubmitChanges();
            return tnew.Image;

        }

        public static void Insert(Image_VideoModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityImage_Video  tnew = new EntityImage_Video ();

            tnew.Image  = Mtnew.Image ;
            tnew.IdVideo = Mtnew.IdVideo;
            tnew.IdQuestion = Mtnew.IdQuestion;
            tnew.Date = DateTime.Now;

         
            db.EntityImage_Videos.InsertOnSubmit(tnew);
           
            db.SubmitChanges();

        }
        public static void InsertEmpty(int IdVideo)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityImage_Video tnew = new EntityImage_Video();

            tnew.Date = DateTime.Now;
            tnew.IdVideo = IdVideo;

            db.EntityImage_Videos.InsertOnSubmit(tnew);
            db.SubmitChanges();
        }

        public static Image_VideoModel LayModel(int IdQVideo)
        {
            Image_VideoModel Mtnew = new Image_VideoModel();


            dbVideoDataContext db = new dbVideoDataContext();

            EntityImage_Video tnew = db.EntityImage_Videos.Where(p => p.Id == IdQVideo).Single();

            var query = from p in db.EntityQuestions_MultipleChoices
                        where p.IdVideo == tnew.IdVideo 
                        select p;
            Mtnew.DSQuestion = query.ToList();
            Mtnew.Id = tnew.Id;
            Mtnew.Image = tnew.Image;
            Mtnew.IdVideo = (int) tnew.IdVideo;
            Mtnew.IdQuestion = (int)tnew.IdQuestion;

            return Mtnew;

        }
        public static string Edit(Image_VideoModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityImage_Video tnew = db.EntityImage_Videos.Where(p => p.Id == Mtnew.Id).Single();

          
            string image = null;
            if (Mtnew.Image != null)
            {
                image = tnew.Image;
                tnew.Image = Mtnew.Image;
            }

            tnew.IdVideo = Mtnew.IdVideo;
            tnew.IdQuestion = Mtnew.IdQuestion;
            tnew.Date = DateTime.Now;

            db.SubmitChanges();
            return image;
            

        }

    }

    
}